/**
 * File: RES.js
 * Created Date: Fri Mar 31 2017
 * Author: Rojer
 * Description: 
 * 
 * Copyright (c) 2017 QBGames
 */

(function () {
    let RES = {}

    // 从resources中加载并设置一个Sprite组件的SpriteFrame属性

    let loadFrameAssets = function (spr, sfPath, succCallback, ignorSamll) {

        let nodeSize = spr.node.getContentSize();

        let reLCount = 2;
        let lfgFrame = null;
        let lfgSmallFrame = null;

        let endFunc = function () {
            let frame = null
            frame = lfgSmallFrame ? (lfgFrame ? lfgFrame : lfgSmallFrame) : lfgFrame;

            if (lfgFrame && lfgSmallFrame) {
                let lSize = lfgFrame.getOriginalSize();
                let sSize = lfgSmallFrame.getOriginalSize();
                //只按宽比较  因为小图资源 等比缩小
                let lsc = Math.abs(lSize.width - nodeSize.width);
                let ssc = Math.abs(sSize.width - nodeSize.width);
                frame = lsc < ssc ? lfgFrame : lfgSmallFrame;
            }
            lfgFrame = null;
            lfgSmallFrame = null;
            if (succCallback) succCallback(frame);

        }
        //默认的资源
        let lfgFunc = function (err, frame) {
            if (!err && frame) {                
                lfgFrame = frame;
            }
            reLCount -= 1;
            if (reLCount == 0) endFunc();
        }

        //查找小图资源
        let lfgSmallFunc = function (err, frame) {
            if (!err && frame) {
                lfgSmallFrame = frame;
            }
            reLCount -= 1;            
            if (reLCount == 0) endFunc();
        }

        //加载默认
        cc.loader.loadRes(sfPath, cc.SpriteFrame, function (err, sf1) {
            lfgFunc(err, sf1);
        });

        if (ignorSamll) {
            lfgSmallFunc(0, null);
            return;
        };

        let smallUrl = sfPath + "_small";

        // let cUrl = cc.Asset.rawUrl(smallUrl);
        // if(!cUrl || cUrl === ""){
        // 	lfgSmallFunc(null);
        // 	return;
        // }
        //加载小图资源
        cc.loader.loadRes(smallUrl, cc.SpriteFrame, function (err, sf2) {
            lfgSmallFunc(err, sf2);
        });
    };

    RES.changeSF = function (sprite, sfPath, succCallback) {

        let spr = sprite;
        if (!spr || sfPath == null || sfPath == "") return;

        loadFrameAssets(sprite, sfPath, function (frame) {
            if (spr.node != null && cc.isValid(spr.node) && frame) {
                spr.spriteFrame = frame;
                if (succCallback) succCallback();
            }
        });

    };

    //忽略读取小图资源
    RES.changeSFIgnSM = function (sprite, sfPath, succCallback) {

        let spr = sprite;
        if (!spr || sfPath == null || sfPath == "") return;

        loadFrameAssets(sprite, sfPath, function (frame) {
            if (spr.node != null && cc.isValid(spr.node)) {
                spr.spriteFrame = frame;
                if (succCallback) succCallback();
            }
        }, true);

    };
    module.exports = RES;
})();